package com.ctrip.cap.client.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ClientProperties;

import com.ctrip.cap.client.BenchmarkServiceClient;
import com.ctrip.cap.client.impl.base.BaseClientImpl;
import com.ctrip.cap.client.impl.base.BaseClientImpl.HttpMethodType;
import com.ctrip.cap.client.property.CapClientProperties;
import com.ctrip.cap.domain.benchmark.BatteryInfo;
import com.ctrip.cap.domain.benchmark.BenchmarkCompatInfo;
import com.ctrip.cap.domain.benchmark.BenchmarkStableInfo;
import com.ctrip.cap.domain.benchmark.CpuRunningInfo;
import com.ctrip.cap.domain.benchmark.MemoryRunningInfo;
import com.ctrip.cap.domain.benchmark.PackageInfo;
import com.ctrip.cap.domain.benchmark.PerformanceSummary;
import com.ctrip.cap.domain.benchmark.SmokeBVTCaseInfo;
import com.ctrip.cap.domain.benchmark.TrafficInfo;
import com.ctrip.cap.domain.benchmark.dashboard.dto.BvtStasticDto;
import com.ctrip.cap.domain.benchmark.query.BenchmarkCompatInfoQueryParam;
import com.ctrip.cap.exception.CapDataException;

public class BenchmarkServiceClientImpl extends BaseClientImpl implements BenchmarkServiceClient{
	
	private static BenchmarkServiceClientImpl client = new BenchmarkServiceClientImpl();
	
	private BenchmarkServiceClientImpl(){
		super();
	}
	
	public static BenchmarkServiceClientImpl getInstance(){
		return client;
	}
	
	@Override
	public void test() {

		Client client = ClientBuilder.newClient();
		WebTarget webTarget = client
				.target(CapClientProperties.capDataUrl)
				.property(ClientProperties.CONNECT_TIMEOUT,
						CapClientProperties.TIMEOUT_MS).path("test");
		Invocation.Builder invocationBuilder = webTarget.request();
		Response response = invocationBuilder.get();
		// http://127.0.0.1:8080/cap-data/service/var?greeting=Hello World!

		if (response.getStatus() != 200) {
			throw new CapDataException(response.readEntity(String.class));
		} else {
			System.out.println(response.readEntity(String.class));
		}
	}

	@Override
	public void addBenchmarkCompatInfo(BenchmarkCompatInfo data) {
		this.baseCall("benchmark/service/addBenchmarkBasic", data, HttpMethodType.PUT);
	}
	
	@Override
	public void addBenchmarkPackagefo(PackageInfo data) {
		this.baseCall("benchmark/service/addBenchmarkPackage", data, HttpMethodType.PUT);
	}

	@Override
	public void addBenchmarkBattery(BatteryInfo batteryInfo) {
		this.baseCall("benchmark/service/addBenchmarkBattery", batteryInfo, HttpMethodType.PUT);
	}
	
	@Override
	public void addBenchmarkCpuDetailInfo(CpuRunningInfo cpuRunningInfo) {
		this.baseCall("/benchmark/service/cpu-detail-info/add", cpuRunningInfo, HttpMethodType.PUT);
	}
	
	@Override
	public void addBenchmarkMemoryDetailInfo(MemoryRunningInfo memoryRunningInfo) {
		this.baseCall("/benchmark/service/memory-detail-info/add", memoryRunningInfo, HttpMethodType.PUT);
	}
	
	@Override
	public void addBenchmarkTraffic(TrafficInfo trafficInfo) {
		this.baseCall("/benchmark/service/traffic/add", trafficInfo, HttpMethodType.PUT);
	}
	
	@Override
	public void queryBenchmarkCompatInfo(
			List<BenchmarkCompatInfoQueryParam> params) {
		this.baseCall("benchmark/service/getBenchmarkCompatInfo", params, HttpMethodType.POST);
	}

	@Override
	public void addPerformanceSummary(List<PerformanceSummary> data) {
		this.baseCall("benchmark/service/addPerformanceSummary", data, HttpMethodType.POST);
	}

	@Override
	public void addBenchmarkStableInfo(BenchmarkStableInfo benchmarkStableInfo) {
		this.baseCall("benchmark/service/addBenchmarkStableInfo", benchmarkStableInfo, HttpMethodType.PUT);
	}
	
	@Override
	public List<SmokeBVTCaseInfo> getSmokeBVTTotallyBadCasesByRunid(long runId) {
		return Arrays.asList(this.baseCall("benchmark/service/smoke-bvt/totally-bad-cases/runid", runId, HttpMethodType.GET, SmokeBVTCaseInfo[].class, "runId"));
	}
	
	@Override
	public BvtStasticDto getBvtStastics(Date limitDate){
		return 	this.baseCall("benchmark/service/stastics", limitDate, HttpMethodType.GET, BvtStasticDto.class, "limit");
		
	}
}
